
using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
using SariturIntranetLibrary.Configuracoes;

namespace SariturIntranetLibrary.Alertas
{
    [Serializable()]
    public class AlertaStatusNameValueList : Csla.NameValueListBase<int, string>
    {
        #region Factory Methods
        private AlertaStatusNameValueList()
        { /* require use of factory method */ }

        private static AlertaStatusNameValueList _list;

        public static AlertaStatusNameValueList GetAlertaStatusNameValueList()
        {
            if (_list == null)
                _list = DataPortal.Fetch<AlertaStatusNameValueList>(new Criteria(typeof(AlertaStatusNameValueList)));
            return _list;
        }

        public static void InvalidateCache()
        {
            _list = null;
        }
        #endregion //Factory Methods


        #region Data Access
        protected override void DataPortal_Fetch(Object criteria)
        {
            RaiseListChangedEvents = false;

            using (SqlConnection cn = new SqlConnection(ConfigDatabase.SariturDBConnection))
            using (SqlCommand cm = cn.CreateCommand())
            {
                cn.Open();

                cm.CommandType = CommandType.StoredProcedure;
                cm.CommandText = "GetAlertaStatusNameValueList";

                using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))
                {
                    IsReadOnly = false;
                    while (dr.Read())
                        this.Add(new NameValuePair(dr.GetInt32("Id"), dr.GetString("Status")));
                    IsReadOnly = true;
                }
            }//using

            RaiseListChangedEvents = true;
        }
        #endregion //Data Access

    }
}
